<template>
  <div class="login-container">
    <div class="login-form-container">
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <h3 class="title">
          用户权限基础平台
        </h3>
        <el-form-item prop="loginName">
          <el-input
            v-model="ruleForm.loginName"
            name="loginName"
            type="text"
            auto-complete="on"
            prefix-icon="el-icon-mobile-phone"
            placeholder="请输入用户名"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="loginPwd" class="login-password">
          <el-input
            v-model="ruleForm.loginPwd"
            :type="pwdType"
            name="loginPwd"
            auto-complete="on"
            placeholder="请输入密码"
            prefix-icon="el-icon-goods"
            @keyup.enter.native="validateSubmit(ruleForm)"
          >
            <i v-if="pwdType==='password'" slot="suffix" class="fa fw fa-eye-slash" @click="toggleShowPwd" />
            <i v-else slot="suffix" class="fa fw fa-eye" @click="toggleShowPwd" />
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="ruleForm.checkedName" size="medium">
            记住用户名
          </el-checkbox>
          <el-checkbox v-model="ruleForm.checkedPassword" size="medium">
            记住密码
          </el-checkbox>
          <a href="#" class="forget-pwd" @click="forgetPwd">[ 忘记密码 ]</a>
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" type="primary" style="width:100%;" @click.native.prevent="validateSubmit(ruleForm)">
            登 录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/mock/index'
import { setStore, getStore, removeStore } from '@/utils/storage'

export default {
  name: 'Login',
  data () {
    return {
      ruleForm: {
        checkedName: true,
        checkedPassword: true
      },
      rules: {
        loginName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        loginPwd: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      loading: false,
      pwdType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created () {
    const loginName = getStore('loginName')
    const loginPwd = getStore('loginPwd')
    if (loginName) {
      this.ruleForm.loginName = loginName
    }
    if (loginPwd) {
      this.ruleForm.loginPwd = loginPwd
    }
  },
  methods: {
    forgetPwd () {
      this.$message({
        message: '请联系管理员',
        type: 'info'
      })
    },
    toggleShowPwd () {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    // 验证
    validateSubmit (ruleForm) {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          this.submit(ruleForm)
        } else {
          return false
        }
      })
    },
    async submit (ruleForm) {
      if (ruleForm.checkedName && ruleForm.checkedPassword) {
        ruleForm.rememberMe = true
      } else {
        ruleForm.rememberMe = false
      }
      this.loading = true
      const res = await login(ruleForm)
      this.loading = false
      if (res.code === '0000') {
        // 写入本地存储
        if (ruleForm.checkedName) {
          setStore('loginName', ruleForm.loginName)
        } else {
          removeStore('loginName')
        }
        if (ruleForm.checkedPassword) {
          setStore('loginPwd', ruleForm.loginPwd)
        } else {
          removeStore('loginPwd')
        }
        setStore('userId', res.data.uuid)
        // 写入token
        // const token = 'token123456'
        this.$store.commit('user/SET_TOKEN', res.data.token)
        this.$message({
          message: '登录成功',
          type: 'success'
        })
        // 重定向到原地址
        this.$router.push({ path: this.redirect || '/' })
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  box-shadow: 5px 5px 5px #fff;
  background: url("~@/assets/images/login/login_bg.jpg") no-repeat center center;
  // background-color: #059fc5;
  .login-form-container {
    width: 480px;
    position: absolute;
    left: 50%;
    top: 50%;
    // transform: translate(-50%, -50%);
    margin-left: -240px;
    margin-top: -240px;
    background-color: rgba(255, 255, 255, 0.2);
    padding: 10px;
    border-radius: 5px;
  }
  .login-form {
    padding: 35px 35px 15px 35px;
    background-color: #fff;
    border-radius: 5px;
  }
  .title {
    font-size: 26px;
    color: #333;
    margin: 0 auto 40px auto;
    text-align: center;
    font-weight: bold;
  }
  .forget-password {
    font-size: 14px;
    color: #3c8dbc;
  }
  .forget-pwd {
    margin-left: 20px;
    color: #3c8dbc;
  }
  .login-password {
    .fa {
      cursor: pointer;
    }
  }
}
</style>
